package com.letwal.cmcvnd.anetwork

import com.letwal.cmcvnd.data.RevUserState
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import org.json.JSONObject
import java.lang.reflect.Type

class ApiUserStateJsonDeserializer : JsonDeserializer<RevUserState> {
    override fun deserialize(
        json: JsonElement,
        typeOfT: Type,
        context: JsonDeserializationContext
    ): RevUserState {
        val result = if (json.isJsonObject) {
            val obj = json.asJsonObject
            val code = obj.get("code").asString
            val msg = obj.get("msg").asString
            val data = JSONObject(obj.getAsJsonObject("obj").toString())
            RevUserState(code, msg, data)
        } else {
            RevUserState()
        }
        return result
    }
}
